
package com.pig4cloud.pigx.wms.service.impl;

import java.util.List;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.wms.entity.SLocation;
import com.pig4cloud.pigx.wms.entity.StockArea;
import com.pig4cloud.pigx.wms.mapper.StockAreaMapper;
import com.pig4cloud.pigx.wms.service.SLocationService;
import com.pig4cloud.pigx.wms.service.StockAreaService;
import com.pig4cloud.pigx.wms.vo.StockAreaDetailForVO;
import com.pig4cloud.pigx.wms.vo.StockAreaForVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@AllArgsConstructor
@Slf4j
public class StockAreaServiceImpl extends ServiceImpl<StockAreaMapper, StockArea> implements StockAreaService {

	private  final StockAreaMapper stockAreaMapper;

	private final SLocationService sLocationService;


	@Override
	public List<StockAreaForVO> listStockAreaForVO(int tier){

		log.info("查询"+tier+"层库区状况");
		return stockAreaMapper.listStockAreaForVO(tier);
	}

	@Override
	public List<StockArea> listStockAreaByAreaIdForVO(int areaId,int tier) {

		log.info("查询"+areaId+"区域"+tier+"层级库区状况");
		return stockAreaMapper.listStockAreaByAreaIdForVO(areaId,tier);
	}

	@Override
	public StockAreaDetailForVO getStockAreaDetailVO(String stockAreaNo) {
		log.info("查询"+stockAreaNo+"库区详情");
		return stockAreaMapper.getStockAreaDetailVO(stockAreaNo);
	}

	@Override
	public boolean activatedStatus(String stockAreaNo, String activatedStatus) {

		if("1".equals(activatedStatus)){

			log.info("将小库区"+stockAreaNo+"变为启用状态.");

			StockArea stockArea = this.getById(stockAreaNo);
			stockArea.setActivatedStatus("1");
			this.updateById(stockArea);

			return true;

		}else{

			log.info("将小库区"+stockAreaNo+"变为停用状态.检查此小库区下所有库位是否都是空库位");
			List<SLocation> sLocations = sLocationService.list(Wrappers.<SLocation>query().lambda().eq(SLocation::getStockAreaNo,
					stockAreaNo).ne(SLocation::getLocationStatus,"1"));

			if(sLocations.size() != 0){

				log.warn("找到小库区"+stockAreaNo+"包含非空库位.不能停用此小库区");
				return false;
			}else{

				log.warn("没找到小库区"+stockAreaNo+"包含非空库位.可以停用此小库区");
				StockArea stockArea = this.getById(stockAreaNo);
				stockArea.setActivatedStatus("0");
				this.updateById(stockArea);
				return true;

			}

		}
	}

}
